<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="用户手机号">
              <a-input-number style="width:100%" placeholder="请输入用户手机号" v-model="queryParam.mobile"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="商家名称">
              <a-input placeholder="请输入商家名称" v-model="queryParam.merchantName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="订单号">
              <a-input-number style="width:100%" placeholder="请输入订单号" v-model="queryParam.orderId"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="商品名称">
              <a-input placeholder="请输入商品名称" v-model="queryParam.goodsName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="下单时间">
              <a-range-picker @change="panelChange" v-model="queryParamRangePicker"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="状态">
              <a-select default-value="1" placeholder="全部状态" v-model="queryParam.status">
                <a-select-option :value="item.val" v-for="(item, index) in statusUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="4" :md="4" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->


    <!-- table区域-begin -->
    <div>
      <a-table 
        ref="table" 
        size="middle" 
        bordered 
        rowKey="id" 
        :scroll="{ x: 1300 }"
        :columns="columns" 
        :dataSource="tableData" 
        :pagination="ipagination" 
        :loading="loading" 
        @change="handleTableChange" >
        <!--操作-->
        <span slot="action" slot-scope="text, record" >
          <a-button type="primary" @click="handleDetail(record)">详情</a-button>
          <!-- <a-button type="primary" @click="handleRefund(record)">极速退款</a-button> -->
        </span>
        <!--图片预览块-->
        <span slot="imgPath" slot-scope="text, record">
          <img :src="record.goodsImg" width="100px" height="100px" />
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <refund-modal ref="modalForm"></refund-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import refundModal from './modules/refundModal'
import ApiTree from "@/api/order/orderList";
import { Slime, crud } from '@/mixins/slime';

export default {
  name: 'orderList',
  mixins: [Slime],
  components: {
    refundModal,
  },
  data() {
    return {
      statusUl: [
        { name: "未支付", val: 0 },
        { name: "已取消", val: 1 },
        { name: "待使用", val: -2 },
        // { name: "已支付", val: 2 },
        // { name: "已发货", val: 3 },
        { name: "发起退款", val: 4 },
        { name: "退款失败", val: 5 },
        { name: "已退款", val: 7 },
        { name: "待评价", val: 8 },
        { name: "已完成", val: 9 },
      ],
      tableData: [],
      columns: [
        {
          title: "订单号",
          align: "center",
          dataIndex: "id",
        },
        {
          title: "商家名称",
          align: "center",
          dataIndex: "merchantName",
        },
        {
          title: "商品名称",
          align: "center",
          dataIndex: "goodsName",
        },
        {
          title: "商品图片",
          align: "center",
          dataIndex: "goodsImg",
          scopedSlots: { customRender: 'imgPath' },
        },
        {
          title: "数量",
          align: "center",
          dataIndex: "count",
        },
        {
          title: "项目单价",
          align: "center",
          dataIndex: "unitPrice",
          customRender: function(money) {
            return (parseInt(money) / 100).toFixed(2);
          }
        },
        {
          title: "实际支付",
          align: "center",
          dataIndex: "payMoney",
          customRender: function(money) {
            return (parseInt(money) / 100).toFixed(2);
          }
        },
        {
          title: "下单用户名",
          align: "center",
          dataIndex: "nickname",
        },
        {
          title: "下单手机",
          align: "center",
          dataIndex: "mobile",
        },
        {
          title: "下单时间",
          align: "center",
          dataIndex: "createTime",
        },
        {
          title: "订单状态",
          align: "center",
          dataIndex: "status",
          customRender: function (status) {
            switch (status) {
              case 0:
                return "未支付";
              case 1:
                return "已取消";
              case -2:
                return "待使用";  
              case 2:
                return "已支付";
              case 3:
                return "已发货";
              case 4:
                return "发起退款";
              case 5:
                return "退款失败";
              case 7:
                return "已退款";
              case 8:
                return "待评价";
              case 9:
                return "已完成";  
              default:
                return "未知状态";
            }
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
    }
  },
  created() {
    crud(ApiTree);
    this.loadData();
  },
  methods:{
    // 加载表格数据
    loadData(arg) {
      this.loadDataPack(arg).then((res) => {
        let tableData = [];
        if (res.result) {
          tableData = res.result.records;
        }
        this.tableData = tableData;
      });
    },
    //详情
    handleDetail(record){
      this.$router.push({ path: '/orderDetail?id='+record.id})
    },
    //退款
    handleRefund(record){
      this.$refs.modalForm.open()
    },
    //选择日期
    panelChange(date){
      let startDate = moment(date[0]).format('YYYY-MM-DD');
      let endDate = moment(date[1]).format('YYYY-MM-DD');
      this.queryParam = {...this.queryParam, startCreateDate:startDate, endCreateDate:endDate}
    },
  }  
}
</script>
<style scoped>
</style>